package com.lingyuan.discounts.controller;

import com.alibaba.fastjson.JSON;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.sms.UpdateCouponHistoryParam;
import com.lingyuan.discounts.domain.Coupon;
import com.lingyuan.discounts.domain.request.CouponHistoryRequest;
import com.lingyuan.discounts.domain.CouponHistory;
import com.lingyuan.discounts.domain.CouponHistoryResponse;
import com.lingyuan.discounts.service.CouponHistoryService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: Li Hao Xuan
 * @name: CouponHistoryController
 * @Description TODO
 */
@Log4j2
@RestController
@RequestMapping("/history")
public class CouponHistoryController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CouponHistoryService couponHistoryService;

    /**
     * 多条件查询用户领取记录表
     * @param couponHistoryRequest
     * @return
     */
    @PostMapping("/findHistoryList")
    public R<List<CouponHistoryResponse>> findHistoryList(@RequestBody CouponHistoryRequest couponHistoryRequest){
        log.info("查询用户领取记录表,方法:{},路径:{},参数:{}",
                request.getMethod(),request.getRequestURI(), JSON.toJSONString(couponHistoryRequest));
        List<CouponHistoryResponse> list = couponHistoryService.findHistoryList(couponHistoryRequest);
        log.info("查询用户领取记录表,方法:{},路径:{},参数:{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(list));
        return R.ok(list);
    }

    /**
     * 新增用户领取记录
     * @param couponHistory
     * @return
     */
    @PostMapping("/addHistory")
    public R addHistory(@RequestBody CouponHistory couponHistory){

        couponHistoryService.addHistory(couponHistory);
        log.info("新增用户领取记录,方法:{},路径:{},参数:{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(couponHistory));
        return R.ok();
    }

    /**
     * 修改用户领取记录
     * @param couponHistory
     * @return
     */
    @PutMapping("/updHistory")
    public R updHistory(@RequestBody CouponHistory couponHistory){
        couponHistoryService.updHistory(couponHistory);
        log.info("修改用户领取记录,方法:{},路径:{},参数:{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(couponHistory));
        return R.ok();
    }

    /**
     * 删除用户领取记录
     * @param id
     * @return
     */
    @DeleteMapping("/delHistory/{id}")
    public R delHistory(@PathVariable("id") Long id){
        couponHistoryService.delHistory(id);
        log.info("删除用户领取记录,方法:{},路径:{},参数:{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(id));
        return R.ok();
    }

    /**
     * 使用优惠券
     * @param param
     * @return
     */
    @PutMapping("/updHistoryStatus")
    public R updHistoryStatus(@RequestBody UpdateCouponHistoryParam param){
        couponHistoryService.updHistoryStatus(param);
        log.info("修改用户领取记录状态,方法:{},路径:{},参数:{},{}",
                request.getMethod(),request.getRequestURI(),JSON.toJSONString(param));
        return R.ok();
    }


    @GetMapping("/findCouponId")
    public R<List<Coupon>> findCouponId(@PathVariable("/id") Long id){
        List<Coupon> list = couponHistoryService.findCouponId(id);
        return R.ok(list);
    }
}
